package com.dpiinc.TechPage.api;

import android.util.Log;

import com.dpiinc.TechPage.AIWriteApplication;
import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.user.utils.ToastUtils;
import com.dpiinc.TechPage.user.utils.Utils;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by hu489_000 on 2018-07-05.
 */

public class RxSchedulers {

    private static final String TAG = "RxSchedulers";
    public static <T> ObservableTransformer<T, T> compose() {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> observable) {
                return observable
                        .subscribeOn(Schedulers.io())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(Disposable disposable) throws Exception {
                                if (!Utils.isNetworkAvailable()) {
                                    Log.d(TAG, "RxSchedulers accept Utils.isNetworkAvailable() = " + Utils.isNetworkAvailable());
                                    ToastUtils.showTopTip(AIWriteApplication.Companion.instance(),AIWriteApplication.Companion.instance().getResources().getString(R.string.connect_net));
                                    return;
                                }
                            }
                        })
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }
}

